<?php
/**
 * Created by PhpStorm.
 * User: boye009
 * Date: 2016/3/22
 * Time: 15:49
 */

namespace Bbs\Api;

use Bbs\Model\ForumAdmingroupModel;
use Bbs\Model\ForumUserAdmingroupModel;
use Common\Api\Api;
use Ucenter\Api\MemberApi;

class ForumUserAdmingroupApi extends Api{

    /**
     * 添加
     */
    const ADD = "Bbs/ForumUserAdmingroup/add";
    /**
     * 保存
     */
    const SAVE = "Bbs/ForumUserAdmingroup/save";
    /**
     * 保存根据ID主键
     */
    const SAVE_BY_ID = "Bbs/ForumUserAdmingroup/saveByID";

    /**
     * 删除
     */
    const DELETE = "Bbs/ForumUserAdmingroup/delete";

    /**
     * 查询
     */
    const QUERY = "Bbs/ForumUserAdmingroup/query";
    /**
     * 查询一条数据
     */
    const GET_INFO = "Bbs/ForumUserAdmingroup/getInfo";

    /**
     * 插入多条数据
     */
    const ADD_ALL = "Bbs/ForumUserAdmingroup/addAll";

    /**
     * 无分页查询
     */
    const QUERY_NO_PAGING = "Bbs/ForumUserAdmingroup/queryNoPaging";

    /**
     * 关小黑屋
     */
    const BAN_USER = "Bbs/ForumUserAdmingroup/banUser";


    /**
     * 删回帖
     */
    const DELETE_POST ="Bbs/ForumUserAdmingroup/deletePost";


    /**
     * 权限查询
     */
    const POWER = "Bbs/ForumUserAdmingroup/power";

    protected function _init(){
        $this -> model = new ForumUserAdmingroupModel();
    }

    /**
     * 删回帖
     */
    public function deletePost($admin_uid,$fid,$tid,$pid){
        $result = $this->power($admin_uid);
        if($result['status']){
            $power = $result['info'];
            if($power[ForumAdmingroupModel::ALLOW_DEL_POST]!=1){
                return $this->apiReturnErr('该用户没有删帖权限');
            }elseif($power['fid']!=0 && $power['fid']!=$fid){
                return $this->apiReturnErr('该用户没有在该论坛的删帖权限');
            }

            $map = array(
                'fid' => $fid,
                'tid' => $tid,
                'pid' => $pid
            );

            $result = apiCall(ForumPostApi::SAVE,array($map,array('status'=>0)));

            if($result['status']){
                return $this->apiReturnSuc('删除成功');
            }else{
                return $this->apiReturnErr('删除失败');
            }

        }else{
            return $this->apiReturnErr($result['info']);
        }

    }


    /**
     * 关小黑屋
     */
    public function banUser($admin_uid,$uid,$banned){

        $result = $this->power($uid);
        if($result['status']){
            $power = $result['info'];
            $admin_group = array(
                ForumAdmingroupModel::GROUP_SUPER_MODERATOR,
                ForumAdmingroupModel::GROUP_MODERATOR
            );
            if(in_array($power['adminid'],$admin_group)){
                return $this->apiReturnErr('不能关管理人员的小黑屋');
            }
        }

        if($admin_uid==$uid){
            return $this->apiReturnErr('admin_uid不能与uid相同');
        }

        $result  = $this->power($admin_uid);
        if($result['status']){
            $power = $result['info'];
            if($power[ForumAdmingroupModel::ALLOW_BAN_USER]!=1){
                return $this->apiReturnErr('该管理人员id没有权限关小黑屋');
            }
        }else{
            return $this->apiReturnErr($result['info']);
        }


        $result = apiCall(MemberApi::GET_INFO,array(array('uid'=>$uid)));

        if($result['status']){
            if(is_null($result['info'])){
                return $this->apiReturnSuc('该用户不存在');
            }
        }else{
            return $this->apiReturnErr($result['info']);
        }

        $model = $this->model;

        if($banned==0){
            //取消关小黑屋
            $map = array(
                'uid' => $uid,
                'adminid' => ForumAdmingroupModel::GROUP_BANNED_USER
            );
            $result = $model->where($map)->delete();

            if($result===false){
                $error = $this -> model -> getDbError();
                return $this -> apiReturnErr($error);
            }else{
                return $this->apiReturnSuc('取消小黑屋成功');
            }

        }elseif($banned==1){
            //关小黑屋
            $entity = array(
                'uid' => $uid,
                'adminid' => ForumAdmingroupModel::GROUP_BANNED_USER,
                'fid' => $power['fid']
            );
            $result = $model->add($entity,array(),true);

            if($result===false){
                $error = $this -> model -> getDbError();
                return $this -> apiReturnErr($error);
            }else{
                return $this->apiReturnSuc('关小黑屋成功');
            }
        }else{
            return $this->apiReturnErr('banned参数错误');
        }



    }


    public function power($uid){
        $result = apiCall(MemberApi::GET_INFO,array(array('uid'=>$uid)));
        if($result['status']){
            if(is_null($result['info'])){
                return $this->apiReturnErr('该管理用户不存在');
            }
        }else{
            return $this->apiReturnErr($result['info']);
        }

        $result = apiCall(ForumUserAdmingroupApi::GET_INFO,array(array('uid'=>$uid)));

        if($result['status']){

            if(is_null($result['info'])){
                return $this->apiReturnSuc('该用户没有特殊权限');
            }else{
                $adminid = $result['info']['adminid'];
                $fid = $result['info']['fid'];
            }

        }else{
            return $this->apiReturnErr('获取用户权限失败');
        }

        $result = apiCall(ForumAdmingroupApi::GET_INFO,array(array('adminid'=>$adminid)));

        if($result['status']){

            if(is_null($result['info'])){
                return $this->apiReturnSuc('该用户没有特殊权限');
            }else{
                $result['info']['fid'] = $fid;
                return $this->apiReturnSuc($result['info']);
            }

        }else{
            return $this->apiReturnErr('获取用户权限失败');
        }

    }


}